package com.bigknow.appstore.proxy.entity;

import com.bigknow.appstore.proxy.enumerate.CallState;
import com.bigknow.appstore.proxy.enumerate.ErrorType;
import com.bigknow.appstore.web.framework.entity.BaseEntity;
import com.bigknow.appstore.web.framework.entity.IEntity;
import com.bigknow.appstore.web.framework.entity.QueryField;
import org.mongodb.morphia.annotations.Entity;
import org.mongodb.morphia.annotations.Transient;

import java.util.Date;

/**
 * proxy的日志记录类
 * Created by chaos on 2016/9/12.
 */
@Entity("web.appstore.proxy.log")
public class ProxyLog extends BaseEntity implements IEntity {

    //认证通过的用户日志才有userId
    private String userId;

    //如果是用户删除，用于显示名称
    @QueryField
    private String userName;

    private String clientIp;

    private Date occurTime;
    private int occurYear;
    private int occurMonth;
    private int occurDay;

    @Transient
    private String tempTime;

    //花费时间
    private int callCostTime;

    private long dataSize;

    private String requestContent;

    private String responseContent;

    private String uri;

    @Transient
    private String tempUri;

    /**
     * 资源id
     */
    private String proxyResId;

    /**
     * 资源（app）名称
     */
    @QueryField
    private String resName;

    /**
     * 日志类型，是正常日志还是错误日志
     */
    private CallState logType;

    @QueryField
    private String logTypeName;

    /**
     * 错误日志类型，只有日志类型为错误的时候才有值
     */
    private ErrorType errorType;

    private String message;

    public ProxyLog(){
        this.setOrderBy("-occurTime");
    }

    public String getUserId() {
        return userId;
    }

    public void setUserId(String userId) {
        this.userId = userId;
    }

    public String getUserName() {
        return userName;
    }

    public void setUserName(String userName) {
        this.userName = userName;
    }

    public String getClientIp() {
        return clientIp;
    }

    public void setClientIp(String clientIp) {
        this.clientIp = clientIp;
    }

    public Date getOccurTime() {
        return occurTime;
    }

    public void setOccurTime(Date occurTime) {
        this.occurTime = occurTime;
    }

    public int getOccurYear() {
        return occurYear;
    }

    public void setOccurYear(int occurYear) {
        this.occurYear = occurYear;
    }

    public int getOccurMonth() {
        return occurMonth;
    }

    public void setOccurMonth(int occurMonth) {
        this.occurMonth = occurMonth;
    }

    public int getOccurDay() {
        return occurDay;
    }

    public void setOccurDay(int occurDay) {
        this.occurDay = occurDay;
    }

    public int getCallCostTime() {
        return callCostTime;
    }

    public void setCallCostTime(int callCostTime) {
        this.callCostTime = callCostTime;
    }

    public long getDataSize() {
        return dataSize;
    }

    public void setDataSize(long dataSize) {
        this.dataSize = dataSize;
    }

    public String getRequestContent() {
        return requestContent;
    }

    public void setRequestContent(String requestContent) {
        this.requestContent = requestContent;
    }

    public String getResponseContent() {
        return responseContent;
    }

    public void setResponseContent(String responseContent) {
        this.responseContent = responseContent;
    }

    public String getUri() {
        return uri;
    }

    public void setUri(String uri) {
        this.uri = uri;
    }

    public String getProxyResId() {
        return proxyResId;
    }

    public void setProxyResId(String proxyResId) {
        this.proxyResId = proxyResId;
    }

    public CallState getLogType() {
        return logType;
    }

    public void setLogType(CallState logType) {
        this.logType = logType;
    }

    public ErrorType getErrorType() {
        return errorType;
    }

    public void setErrorType(ErrorType errorType) {
        this.errorType = errorType;
    }

    public String getMessage() {
        return message;
    }

    public void setMessage(String message) {
        this.message = message;
    }

    public String getTempTime() {
        return tempTime;
    }

    public void setTempTime(String tempTime) {
        this.tempTime = tempTime;
    }

    public String getResName() {
        return resName;
    }

    public void setResName(String resName) {
        this.resName = resName;
    }

    public String getLogTypeName() {
        if (logTypeName == null) {
            setLogTypeNameByType();
        }
        return logTypeName;
    }

    public void setLogTypeNameByType() {
        CallState logType = this.getLogType();
        if (CallState.SUCCESS.equals(logType)) {
            setLogTypeName("调用成功");
        } else {
            setLogTypeName(this.getErrorType().getMessage());
        }
    }


    public void setLogTypeName(String logTypeName) {
        this.logTypeName = logTypeName;
    }

    public String getTempUri() {
        if (this.getUri() != null && getUri().length() > 25) {
            return getUri().substring(0, 22) + "...";
        } else {
            return getUri();
        }
    }

    public void setTempUri(String tempUri) {
        this.tempUri = tempUri;
    }
}
